<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border row-key="id" :data="list">
          <el-table-column label="名称" prop="name"/>
          <el-table-column label="标识" prop="code"/>
          <el-table-column label="描述" prop="description"/>
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" v-if="row.type===1" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text" @click="doEdit(row)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>


      <el-dialog :visible.sync="showDialog" title="弹层标题" @close="resetForm">
  <!-- 表单内容 -->
  <el-form label-width="100px" ref="ref" :model="formData" :rules="rules">
    <el-form-item label="权限名称" prop="name">
      <el-input v-model="formData.name" />
    </el-form-item>
    <el-form-item label="权限标识" prop="code">
      <el-input v-model="formData.code" />
    </el-form-item>
    <el-form-item label="权限描述">
      <el-input v-model="formData.description" />
    </el-form-item>
    <el-form-item label="权限启用">
      <el-switch
        v-model="formData.enVisible"
        active-text="启用"
        active-value="1"
        inactive-text="不启用"
        inactive-value="0"
      />
    </el-form-item>
  </el-form>

  <template #footer>
    <div style="text-align: right;">
      <el-button @click="showDialog = false">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </div>
  </template>
</el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList,addPermission,delPermission ,updatePermission} from "@/api/permissions";
import { tranDataToTree } from "@/utils";
export default {
  data() {
    var validatePass = (rule, value, callback) =>{
      console.log(rule);
      console.log(value);
      let add= this.originList.map(item=> item.code)
      console.log(add);
      if(this.isEdit){
        add = this.originList.filter(item=>item.code!==this.formData.code)
      }

      if(add.includes(value)){
        callback(new Error('code值重复'));
      }else{
        callback()
      }
    }

    var validates = (rule, value, callback) =>{
      console.log(rule);
      console.log(value);
      let ass= this.originList.filter(item => item.pid!==this.formData.pid).map(item=>item.name)
      // this.originList.filter(item => item.pid === this.formData.pid)
      console.log(ass);
      // if(this.isEdit){
      //   add = this.originList.filter(item=>item.name!==this.formData.name)
      // }

      if(ass.includes(value)){
        callback(new Error('name重复'));
      }else{
        callback()
      }
    }
    return {
      isEdit:false,
      list: [],
       showDialog: false, // 是否显示弹层
       originList:{},
    formData: {
      name: '', // 名称
      code: '', // 权限标识
      description: '', // 描述
      enVisible: '0', // 开启
      pid: '', // 添加到哪个节点下
      type: '' // 类型
    },
    rules:{
      name: [
        { validator: validates, trigger: 'blur' },
        { required: true, message: '请输入活动名称', trigger: 'blur' }
      ], // 名称
      code: [
        { validator: validatePass, trigger: 'blur' },
        { required: true, message: '请输入活动名称', trigger: 'blur' }
      ], // 权限标识
      
    }
    };
  },
  created(){
    this.loadPermissionList()
  },
  methods: {
    // 更新权限\
   async hEdit(){
     await updatePermission(this.formData)
     this.loadPermissionList()
     this.showDialog= false
    },
    // 编辑
    doEdit(row){
      this.isEdit=true
      this.showDialog=true
      this.formData={...row}
    },
    // 删除
    hDel(id){
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delPermission(id)
          this.loadPermissionList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 重置表单
    resetForm(){
      this.$refs.ref.resetFields()
      this.formData= {
      name: '', // 名称
      code: '', // 权限标识
      description: '', // 描述
      enVisible: '0', // 开启
      pid: '', // 添加到哪个节点下
      type: '' // 类型
    }

    },
    // 新增权限
   async doAdd(){
      try {
        await addPermission(this.formData)
        this.loadPermissionList()
        this.showDialog =false
      } catch (error) {
        console.log(error);
      }
    },
    // 确定
    hSubmit(){
      this.isEdit?this.hEdit():this.doAdd()
      
      
    },
    hAdd(type,pid){
      this.formData.pid=pid
      this.formData.type=type
      this.isEdit=false
      this.showDialog=true
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList();
        console.log("getPermissionList", res);
        this.list = tranDataToTree(res.data);
        this.originList = res.data.map(({ id, pid, name, code }) => ({ id, pid, name: name.trim(), code }))
        // 保存数据
      } catch (err) {
        console.log("getPermissionList", err);
      }
    },
  },
};
</script>
